﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using UserSurface;
using ApplicationCore;

namespace EnzymeLaborTool
{
    public partial class UnitFrm : KryptonForm
    {
        public UnitFrm()
        {
            InitializeComponent();
        }

        private MainSurface _surface;
        private EnzymeDBDataContext _dataContext;
        public void Init(MainSurface ms)
        {
            _surface = ms;
            _dataContext = ms.GetDataContext();
            BindUnits();
        }

        private void BindUnits()
        {
            var q = from x in _dataContext.Units orderby x.Name select x;
            unitBindingSource.DataSource = q;
        }

        private void kryptonDataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (unitDataGridView.IsCurrentCellInEditMode)
            {
                var unit = unitDataGridView.Rows[e.RowIndex].DataBoundItem as Unit;
                if (unit == null)
                {
                    return;
                }
                string val = e.FormattedValue.ToString().Trim().ToLower();
                if (string.IsNullOrEmpty(val))
                {
                    unitDataGridView.Rows[e.RowIndex].ErrorText = "Unit can't be empty.";
                    e.Cancel = true;
                }
                else if (_dataContext.Units.FirstOrDefault(x => x.UId != unit.UId && x.Name.ToLower().Trim() == val) != null)
                {
                    unitDataGridView.Rows[e.RowIndex].ErrorText = "This unit already exist.";
                    e.Cancel = true;
                }
            }
        }

        private void unitDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            Unit av = null;
            unitDataGridView.Rows[e.RowIndex].ErrorText = null;

            try
            {
                av = unitDataGridView.Rows[e.RowIndex].DataBoundItem as Unit;
            }
            catch (IndexOutOfRangeException)
            {
                //handles case where new row is edited and then escape key is pressed to cancel the new row
            }

            if (av != null)
            {
                _dataContext.SubmitChanges();
            }
        }

        private void unitBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Unit { UId = Guid.NewGuid() };
        }

        private void unitDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show("You are about to delete the current unit and the information an all analytes where the unit is used. Do you want proceed?", "Delete unit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                == System.Windows.Forms.DialogResult.Cancel)
                e.Cancel = true;
        }

        private void unitDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            _dataContext.SubmitChanges();
        }


    }
}
